package com.example.teamtribe.main.club.clubmembers;

import android.os.Bundle;
import android.support.annotation.Nullable;

import com.example.teamtribe.common.Back;
import com.example.teamtribe.common.model.UserModel;
import com.example.teamtribe.database.User;
import com.example.teamtribe.utils.Utility;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lenovo on 2018/5/27.
 */

public class MembersPresenter implements MembersContract.Presenter{
    private MembersContract.View view;
    private MembersModel model;

    public MembersPresenter(MembersContract.View view) {
        this.view = view;
        model = new MembersModel();
    }

    @Override
    public void getMembers(final int teamId, int offset, int num) {
        model.getPresident(teamId, membersBack(1));//1为会长，2为管理，3为普通成员
        model.getAdmins(teamId, membersBack(2));
        model.getMembers(teamId,offset,num,membersBack(3));
    }
    @Override
    public void getMore(final int teamId, int offset, int num) {
        model.getMembers(teamId,offset,num,membersBack(3));
    }

    @Override
    public void addRole(final int teamId, final int roleId) {
        model.addRole(teamId, roleId, new Back() {
            @Override
            public void success(@Nullable Bundle bundle) {
                getMembers(teamId,0,20);
            }

            @Override
            public void failure(String meg) {
                view.showToast(meg);
            }
        });
    }

    private Back membersBack(final int type){//这个地方改了！！！！！
        return new Back() {
            @Override
            public void success(@Nullable Bundle bundle) {
                ArrayList<Integer> ids = bundle.getIntegerArrayList("ids");
                view.clearMembers();
                if(type==1) {
                    if (ids.isEmpty()) {
                        view.finishRefresh();
                        return;
                    }
                    for (int i = 0; i < ids.size(); i++) {
                        getMember(ids.get(i), type);
                        if (i == ids.size() - 1) {
                            view.finishRefresh();
                        }
                    }
                }else if(type==2) {
                    List<User> admins = DataSupport.where("realId > ? and realId < ?", "100000", "100004").find(User.class);
                    for (int i = 0; i < admins.size(); i++) {
                        view.addMember(admins.get(i), 2);
                    }
                }else{
                    List<User> members=DataSupport.where("realId > ? and realId < ?", "100000","100040").find(User.class);
                    for(int i=0;i<members.size();i++){
                        view.addMember(members.get(i),3);
                    }
                }

            }

            @Override
            public void failure(String meg) {
                view.finishRefresh();
                view.showToast(meg);
            }
        };
    }

    public void getMember(int userId,final int type) {
        UserModel.getInstance().getUserNoHead(Utility.getToken(), userId, new Back() {
            @Override
            public void success(@Nullable Bundle bundle) {
                User user = bundle.getParcelable("user");
                view.addMember(user,type);
            }

            @Override
            public void failure(String meg) {
                view.showToast(meg);
                view.finishRefresh();
            }
        });
    }
}


